package com.hopu.oa.controller.task;


import com.hopu.oa.dao.task.TaskDao;
import com.hopu.oa.dao.task.TaskuserDao;
import com.hopu.oa.dao.task.TypeListDao;
import com.hopu.oa.entity.management.AoaStatusList;
import com.hopu.oa.entity.management.AoaTypeList;
import com.hopu.oa.entity.task.AoaTaskLogger;
import com.hopu.oa.entity.task.AoaTaskUser;
import com.hopu.oa.entity.task.AoaTasklist;
import com.hopu.oa.entity.user.AoaUser;

import com.hopu.oa.service.impl.task.TaskServiceImpl;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * @author Secret
 * @create 2021-07-17 上午 09:18
 */
@Controller
public class TaskController {
    @Autowired
    private TaskServiceImpl taskService;
    @Autowired
    TaskDao taskMapper;
    @Autowired
    private TaskuserDao taskuserMapper;
    @Autowired
    private TypeListDao typeListMapper;

    @RequestMapping("taskmanage")
    public String taskmanage(@RequestParam(value = "baseKey", defaultValue = "") String title, @RequestParam(value = "var", defaultValue = "") String var,
                             @RequestParam(value = "page", defaultValue = "1") int page,
                             @RequestParam(value = "size", defaultValue = "10") int size,
                             Model model, HttpSession session) {
        System.out.println(var);
        AoaUser user = (AoaUser) session.getAttribute("user");
        Integer userId = user.getUserId();
        System.out.println(userId);
        int totalElements = taskService.findAllTask(userId, title, page, 0, var).size();
        //有几页
        int totalPages = totalElements % size == 0 ? totalElements / size : totalElements / size + 1;
        List<AoaTasklist> allTask = taskService.findAllTask(userId, title, page, size, var);
        System.out.println(allTask);
        HashMap<String, Object> map = new HashMap<>();
        map.put("totalElements", totalElements);
        map.put("totalPages", totalPages);
        map.put("number", page);
        map.put("size", size);
        map.put("var", var);
        model.addAttribute("page", map);
        model.addAttribute("tasklist", allTask);
        model.addAttribute("baseKey", title);
        model.addAttribute("url", "taskmanages");
        return "task/taskmanage";
    }

    @RequestMapping("taskmanages")
    public String taskmanages(@RequestParam(value = "baseKey", defaultValue = "") String title, @RequestParam(value = "var", defaultValue = "") String var,
                              @RequestParam(value = "page", defaultValue = "1") int page,
                              @RequestParam(value = "size", defaultValue = "10") int size,
                              Model model, HttpSession session) {
        AoaUser user = (AoaUser) session.getAttribute("user");
        Integer userId = user.getUserId();
//        String var = request.getParameter("var");
        int totalElements = taskService.findAllTask(userId, title, page, 0, var).size();
        //有几页
        int totalPages = totalElements % size == 0 ? totalElements / size : totalElements / size + 1;
        List<AoaTasklist> allTask = taskService.findAllTask(userId, title, page, size, var);
        HashMap<String, Object> map = new HashMap<>();
        map.put("totalElements", totalElements);
        map.put("totalPages", totalPages);
        map.put("number", page);
        map.put("size", size);
        map.put("var", var);
        model.addAttribute("page", map);
        model.addAttribute("tasklist", allTask);
        model.addAttribute("baseKey", title);
        model.addAttribute("url", "taskmanages");
        return "task/managetable";
    }


//    ///快捷排序
//    @RequestMapping("paixu")
//    public String paixu(HttpServletRequest request) {
//        String var = request.getParameter("var");
//
//        return "task/managetable";
//    }


    //添加修改
    @RequestMapping("addtask")
    public String addtask(Model model, HttpSession session,
                          String userName, Integer nowPage,

                          @RequestParam(value = "page", defaultValue = "1") int page,
                          @RequestParam(value = "size", defaultValue = "3") int size) {
        List<AoaTypeList> typeLists = taskService.selectByTypelist();
        model.addAttribute("typelist", typeLists);
        List<AoaStatusList> statusLists = taskService.selectBytaskList();
        model.addAttribute("statuslist", statusLists);
        AoaUser user = (AoaUser) session.getAttribute("user");
        //总数据
        int totalElements = taskService.selectByfatherId(user.getUserId(), userName, page, 0).size();
        //有几页
        int totalPages = totalElements % size == 0 ? totalElements / size : totalElements / size + 1;
        List<AoaUser> userInfos = taskService.selectByfatherId(user.getUserId(), userName, (page - 1) * size, size);
        System.out.println(userInfos);
        model.addAttribute("userInfo", userInfos);
        HashMap<String, Integer> map = new HashMap<>();
        map.put("totalElements", totalElements);
        map.put("totalPages", totalPages);
        map.put("number", page);
        map.put("size", size);
        System.out.println(map);
        model.addAttribute("page", map);
        model.addAttribute("nowPage", nowPage);
        model.addAttribute("url", "addtaskkkk");
        return "task/addtask";
    }

    @RequestMapping("addtaskkkk")
    public String addtaskkkk(Model model, HttpSession session,
                             String userName,
                             @RequestParam(value = "page", defaultValue = "1") int page,
                             @RequestParam(value = "size", defaultValue = "3") int size) {
        AoaUser user = (AoaUser) session.getAttribute("user");
        //总数据
        int totalElements = taskService.selectByfatherId(user.getUserId(), userName, page, 0).size();
        //有几页
        int totalPages = totalElements % size == 0 ? totalElements / size : totalElements / size + 1;
        List<AoaUser> userInfos = taskService.selectByfatherId(user.getUserId(), userName, (page - 1) * size, size);
        System.out.println(userInfos);
        model.addAttribute("userInfo", userInfos);
        HashMap<String, Integer> map = new HashMap<>();
        map.put("totalElements", totalElements);
        map.put("totalPages", totalPages);
        map.put("number", page);
        map.put("size", size);
        System.out.println(map);
        model.addAttribute("page", map);
        model.addAttribute("url", "addtaskkkk");
        return "common/recivers";

    }

    @RequestMapping("ck_addtask")
    public String insertTask(AoaTasklist tasklist, @Param("top") String top, HttpSession session) {
        System.out.println(tasklist);
        AoaUser user = (AoaUser) session.getAttribute("user");
        String[] nameList = tasklist.getReciverlist().split(";");
        List<Integer> userIds = new ArrayList<Integer>();
        for (String name : nameList) {
            userIds.add(taskMapper.selectUserByUserName(name).getUserId());
        }
        if ("on".equals(top)) {
            tasklist.setIsTop(1);
        } else {
            tasklist.setIsTop(0);
        }
        tasklist.setTaskPushUserId(user.getUserId());
        int i1 = taskService.insertTaskList(tasklist);
        System.out.println(i1);
        int i = 0;
        for (int j = 0; j < userIds.size(); j++) {

            taskMapper.insertIntotaskUser(tasklist.getStatusId(), tasklist.getTaskId(), userIds.get(j));
            i++;
        }
        System.out.println(tasklist.getStarTime());
        System.out.println(tasklist.getTaskId());
        System.out.println(tasklist.getStatusId());
        taskService.insertintoLogger(tasklist.getStarTime(), tasklist.getTaskId(), user.getUserName(), tasklist.getStatusId());
        System.out.println(userIds);


//            UserInfo user = (UserInfo)session.getAttribute("user");
//            tasklist.setTaskPushUserId(user.getUserId());
//            int i = taskService.insertTaskList(tasklist);
//            System.out.println("-------------------------------------------------");
//            System.out.println(tasklist);
//            taskService.insertIntotaskUser(tasklist.getStatusId(),tasklist.getTaskId(),tasklist.getTaskPushUserId());
        return "redirect:taskmanage";
    }

    @RequestMapping("edittasks")
    public String edittasks(@Param("id") int id, Model model,
                            String userName, Integer nowPage,
                            HttpSession session,
                            @RequestParam(value = "page", defaultValue = "1") int page,
                            @RequestParam(value = "size", defaultValue = "3") int size) {
        AoaTasklist tasklist = taskService.selectBytaskId(id);

        List<AoaTypeList> typeList = typeListMapper.selectByMap(new HashMap<String, Object>() {{
            put("type_id", tasklist.getTypeId());
        }});
        AoaStatusList statusList = taskService.selectByStatusListId(tasklist.getStatusId());
        model.addAttribute("task", tasklist);
        model.addAttribute("status", statusList);
        model.addAttribute("type", typeList.get(0));
        model.addAttribute("nowPage", nowPage);
        System.out.println(id + "++++++" + typeList);
        AoaUser user = (AoaUser) session.getAttribute("user");
        //总数据
        int totalElements = taskService.selectByfatherId(user.getUserId(), userName, page, 0).size();
        //有几页
        int totalPages = totalElements % size == 0 ? totalElements / size : totalElements / size + 1;
        List<AoaUser> userInfos = taskService.selectByfatherId(user.getUserId(), userName, (page - 1) * size, size);
        System.out.println(userInfos);
        model.addAttribute("userInfo", userInfos);
        HashMap<String, Integer> map = new HashMap<>();
        map.put("totalElements", totalElements);
        map.put("totalPages", totalPages);
        map.put("number", page);
        map.put("size", size);
        System.out.println(map);
        model.addAttribute("page", map);
        model.addAttribute("id", id);
        model.addAttribute("url", "addtaskkkk");
        return "task/edittask";
    }

    @RequestMapping("update")
    public String update(AoaTasklist tasklist, Integer nowPage,
                         @Param("top") String top,
                         HttpSession session) {
        if ("on".equals(top)) {
            tasklist.setIsTop(1);
        } else {
            tasklist.setIsTop(0);
        }
        AoaUser user = (AoaUser) session.getAttribute("user");
        tasklist.setTaskPushUserId(user.getUserId());
        taskService.updateTaskList(tasklist);
        return "redirect:taskmanage?page=" + nowPage;

    }

    @RequestMapping("shanchu")
    public String shanchu(int id, Integer page) {
        taskMapper.deleteTaskListById(id);
        taskMapper.deleteTaskUserById(id);
        taskMapper.deleteTaskLoggerById(id);
        return "redirect:taskmanage?page=" + page;

    }

    @RequestMapping("seetasks")
    public String seetasks(int id, Model model, Integer page) {
        model.addAttribute("id", id);

        System.out.println("page = " + page);
        model.addAttribute("page", page);
//        return "forward:myseetasks";
//        Tasklist task = taskService.queryTasklistById(id);
//        UserInfo user = taskService.queryUserInfoById(task.getTaskPushUserId());
//        StatusList status = taskService.queryStatusListById(task.getStatusId());
//        List<Tasklist> tasklistAll = taskService.findTasklistAll();
//        List<Tasklogger> taskloggerAll = taskService.findTaskloggerAll();
//
//        System.out.println(task);
//        System.out.println(user);
//        System.out.println(status);
//        System.out.println(tasklistAll);
//        System.out.println(taskloggerAll);
//        model.addAttribute("task",task);
//        model.addAttribute("user",user);
//        model.addAttribute("status",status);
//        model.addAttribute("statuslist", tasklistAll);
//        model.addAttribute("loggerlist", taskloggerAll);


        return "forward:myseetasks";
    }


    //我的任务
    @RequestMapping("mytask")
    public String mytaskmanage(@RequestParam(value = "baseKey", defaultValue = "") String title,
                               @RequestParam(value = "page", defaultValue = "1") int page,
                               @RequestParam(value = "size", defaultValue = "5") int size,
                               Model model, HttpSession session) {
        AoaUser user = (AoaUser) session.getAttribute("user");
        Integer userId = user.getFatherId();

        int totalElements = taskService.selectTaskList(userId, title, page, 0).size();
        int totalPages = totalElements % size == 0 ? totalElements / size : totalElements / size + 1;
        List<AoaTasklist> tasklist = taskService.selectTaskList(userId, title, page, size);
        List<AoaTasklist> aoaTasklists = taskuserMapper.selectTaskUser(user.getUserId());

        for (AoaTasklist aoaTasklist : tasklist) {
            System.out.println(aoaTasklist);
        }
        System.out.println("-------------------------------------------------------");
        System.out.println(tasklist);
        HashMap<String, Integer> map = new HashMap<>();
        map.put("totalElements", totalElements);
        map.put("totalPages", totalPages);
        map.put("number", page);
        map.put("size", size);
        model.addAttribute("page", map);
        model.addAttribute("tasklist1", tasklist);
        model.addAttribute("url", "setaskmanage");
        model.addAttribute("baseKey", title);
        model.addAttribute("aoaTaskUsers", aoaTasklists);


        return "task/mytask";

    }

    @RequestMapping("setaskmanage")
    public String setaskmanage(@RequestParam(value = "baseKey", defaultValue = "") String title,
                               @RequestParam(value = "page", defaultValue = "1") int page,
                               @RequestParam(value = "size", defaultValue = "5") int size,
                               Model model, HttpSession session) {
        AoaUser user = (AoaUser) session.getAttribute("user");
        Integer userId = user.getFatherId();
        int totalElements = taskService.selectTaskList(userId, title, page, 0).size();
        int totalPages = totalElements % size == 0 ? totalElements / size : totalElements / size + 1;
        List<AoaTasklist> tasklist = taskService.selectTaskList(userId, title, page, size);
        List<AoaTasklist> aoaTasklists = taskuserMapper.selectTaskUser(user.getUserId());


        HashMap<String, Integer> map = new HashMap<>();
        map.put("totalElements", totalElements);
        map.put("totalPages", totalPages);
        map.put("number", page);
        map.put("size", size);
        model.addAttribute("page", map);
        model.addAttribute("tasklist1", tasklist);
        model.addAttribute("url", "setaskmanage");
        model.addAttribute("baseKey", title);
        model.addAttribute("aoaTaskUsers", aoaTasklists);
//        session.setAttribute("title",title);

        return "task/mytasklist";
    }


    /**
     * 查询页面
     *
     * @param model
     * @return
     */
    @RequestMapping("myseetasks")
    public String findAll(int id, HttpSession session, Model model, Integer nowPage, String remove) {

        AoaTasklist task = taskService.queryTasklistById(id);
        AoaUser user = taskService.queryUserInfoById(task.getTaskPushUserId());
        System.out.println(user);
        AoaStatusList status = taskService.queryStatusListById(task.getStatusId());
        List<AoaTasklist> tasklistAll = taskService.findTasklistAll();
        List<AoaTaskLogger> taskloggerAll = taskService.findTaskloggerAll();

        System.out.println(task);
        System.out.println(user);
        System.out.println(status);
        System.out.println(tasklistAll);
        System.out.println(taskloggerAll);
        model.addAttribute("nowPage", nowPage);
        model.addAttribute("remove", remove);
        model.addAttribute("task", task);
        model.addAttribute("user", user);
        model.addAttribute("status", status);
        model.addAttribute("statuslist", tasklistAll);
        model.addAttribute("loggerlist", taskloggerAll);


        return "task/myseetask";
    }

    @RequestMapping("uplogger")
    public String uplogger(

            @RequestParam(value = "loggerStatusid", defaultValue = "-1") int loggerStatusid,
            @RequestParam(value = "username", defaultValue = "") String username,
            @RequestParam(value = "loggerTicking", defaultValue = "") String loggerTicking,
            @RequestParam(value = "taskId", defaultValue = "0") int taskId,
            @RequestParam(value = "page", defaultValue = "1") int page,
            @RequestParam(value = "size", defaultValue = "5") int size,
            HttpSession session

    ) {
        System.out.println("taskId=" + taskId + "loggerStatusid=" + loggerStatusid + "username=" + username);

        AoaUser user = (AoaUser) session.getAttribute("user");
        if (username.equals(user.getUserName())) {

            taskService.updateLogger(loggerStatusid, loggerTicking, taskId);
            taskService.updateTask(loggerStatusid, taskId);
            taskuserMapper.updateTaskUserByTaskId(loggerStatusid, taskId);
            return "redirect:taskmanage";
        } else {
            taskService.updateTaskUser(loggerStatusid, taskId, user.getUserId());
            if (true) {
                int aoaTaskUser = taskService.querystatusIdById(taskId);
                taskService.updateLogger(aoaTaskUser, loggerTicking, taskId);
                taskService.updateTask(aoaTaskUser, taskId);
            }
            return "redirect:mytask";
        }


    }

    @RequestMapping("myshanchu")
    public String myshanchu(Model model, int id, HttpSession session, Integer nowPage) {
        taskuserMapper.myshanchu(id);
        taskuserMapper.loggershanchu(id);
        taskuserMapper.usershanchu(id);
        return "redirect:mytask?page=" + nowPage;
    }

    @RequestMapping("tasklogger")
    public String tasklogger(Model model, int id, HttpSession session) {

        taskuserMapper.myshanchu(id);
        return "redirect:/mytask";
    }

}
